package com.toman.Adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.Adpter.holder.DeviceRepairHolder;
import com.toman.Enum.BaseEnum;
import com.toman.Enum.PermEnum;
import com.toman.JavaBean.DeviceRepairRecordEntity;
import com.toman.R;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;

import java.util.List;

/**
 * Created by 邹苏隆 on 2018/6/19.
 * description：设备预维护列表适配器
 */

public class DeviceRepairAdapter extends AdapterBase<DeviceRepairRecordEntity,DeviceRepairHolder> implements View.OnClickListener ,CompoundButton.OnCheckedChangeListener {




    public interface ActionClickListener {
        public void onDetailClick(DeviceRepairRecordEntity bean);
//查看详情
        public void onDelClick(DeviceRepairRecordEntity bean);
//删除
        public void onCheckClick(DeviceRepairRecordEntity bean);
//审核
public void onRepairClick(DeviceRepairRecordEntity bean);
//维修
    }

    private ActionClickListener actionClickListener;
    public void setActionClickListener(ActionClickListener l) {
        actionClickListener = l;
    }

    private LayoutInflater inflater;
    private Context mContext;

    public DeviceRepairAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    @Override

    protected void handlerData(List<DeviceRepairRecordEntity> mList2, int position, DeviceRepairHolder itemView) {
        DeviceRepairRecordEntity bean = mList2.get(position);
        itemView.tv_name.setText(bean.getDeviceName()+"("+bean.getDeviceEntity().getDeviceCode()+")");
        itemView.tv_code.setText(bean.getId());
        try {
            itemView.tv_dep.setText(bean.getDeviceEntity().getDepartmentEntity().getDepartmentName());
        } catch (Exception e) {
            e.printStackTrace();
        }
//        itemView.tv_no.setText("("+bean.getDeviceEntity().getDeviceCode()+")");
        itemView.cb_status.setChecked(bean.isSelect_status());
StringBuilder stringBuilder=new StringBuilder();
for (int i=0;i<bean.getProblemItemEntities().size();i++){
    if (stringBuilder.length()<=0){
        stringBuilder.append(bean.getProblemItemEntities().get(i).getItemName());
    }
    else {
        stringBuilder.append("、").append(bean.getProblemItemEntities().get(i).getItemName());
    }
}

        itemView.tv_problem.setText(stringBuilder.toString());
        itemView.tv_beltline.setText(bean.getDeviceEntity().getBeltlineName());

        if (bean.getCheckStatus().equals("0")){
        itemView.tv_time.setText(bean.getCreateTime());
        itemView.tv_tally.setText(bean.getCreateUser().getRealName());
        itemView.iv_status.setImageResource(R.mipmap.icon_dwx);
            itemView.tv_tally_name.setText("申报人");
            itemView.btn_check.setVisibility(View.GONE);
            if (!PermissonUtils.contains(mContext, BaseEnum.REPAIR.has(PermEnum.REPAIR))) {
                itemView.btn_repair.setVisibility(View.GONE);
            }
            else {
                itemView.btn_repair.setVisibility(View.VISIBLE);
            }
            if (!PermissonUtils.contains(mContext, BaseEnum.REPAIR.has(PermEnum.DELETE))) {
                itemView.btn_del.setVisibility(View.GONE);

            }
            else {
                itemView.btn_del.setVisibility(View.VISIBLE);

            }
            itemView.cb_status.setVisibility(View.GONE);
//            获取选中人员

        }
        else if (bean.getCheckStatus().equals("1")){
            itemView.btn_del.setVisibility(View.GONE);
            if (!PermissonUtils.contains(mContext, BaseEnum.REPAIR.has(PermEnum.CHECK))) {
                itemView.btn_check.setVisibility(View.GONE);
                itemView.cb_status.setVisibility(View.GONE);
            }
            else {
                itemView.btn_check.setVisibility(View.VISIBLE);
                itemView.cb_status.setVisibility(View.VISIBLE);

            }
            if (!PermissonUtils.contains(mContext, BaseEnum.REPAIR.has(PermEnum.DELETE))) {
                itemView.btn_del.setVisibility(View.GONE);

            }
            else {
                itemView.btn_del.setVisibility(View.VISIBLE);

            }
            itemView.btn_repair.setVisibility(View.GONE);

            itemView.tv_time.setText(bean.getRepairEndTime());
            itemView.tv_tally_name.setText("维修人");
            try {
                itemView.tv_tally.setText(bean.getRepairUser().getRealName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            itemView.iv_status.setImageResource(R.mipmap.icon_dsh);

        }
        else if (bean.getCheckStatus().equals("2")){
            itemView.btn_del.setVisibility(View.GONE);
            itemView.btn_repair.setVisibility(View.GONE);
            itemView.btn_check.setVisibility(View.GONE);

            itemView.tv_time.setText(bean.getAuditTime());
            itemView.tv_tally_name.setText("审核人");
            itemView.tv_tally.setText(bean.getAuditUser().getRealName());

            itemView.iv_status.setImageResource(R.mipmap.icon_ysh);
            itemView.cb_status.setVisibility(View.GONE);
        }
        itemView.cb_status.setOnCheckedChangeListener(this);
        itemView.cb_status.setTag(position);

        itemView.btn_del.setOnClickListener(this);
        itemView.btn_del.setTag(position);
        itemView.btn_check.setOnClickListener(this);
        itemView.btn_check.setTag(position);
        itemView.btn_detail.setOnClickListener(this);
        itemView.btn_detail.setTag(position);
        itemView.btn_repair.setOnClickListener(this);
        itemView.btn_repair.setTag(position);
    }

    @Override
    protected DeviceRepairHolder getItemViewHolder() {

        return new DeviceRepairHolder();
    }

    @Override
    protected View initConvertView(View convertView, DeviceRepairHolder v) {
        convertView = inflater.inflate(R.layout.item_device_repair, null);
        v.iv_status= (ImageView) convertView.findViewById(R.id.iv_status);
        v.btn_check= (Button) convertView.findViewById(R.id.btn_check);
        v.btn_del= (Button) convertView.findViewById(R.id.btn_del);
        v.btn_detail= (Button) convertView.findViewById(R.id.btn_detail);
        v.btn_repair= (Button) convertView.findViewById(R.id.btn_repair);

        v.tv_name= (TextView) convertView.findViewById(R.id.tv_name);
        v.tv_tally_name = (TextView) convertView.findViewById(R.id.tv_tally_title);
        v.tv_dep= (TextView) convertView.findViewById(R.id.tv_dep);
        v.tv_no= (TextView) convertView.findViewById(R.id.tv_no);
        v.tv_code= (TextView) convertView.findViewById(R.id.tv_code);
        v.tv_tally= (TextView) convertView.findViewById(R.id.tv_tally);
        v.tv_time= (TextView) convertView.findViewById(R.id.tv_time);
        v.cb_status=(CheckBox) convertView.findViewById(R.id.cb_status);
        v.tv_beltline=(TextView) convertView.findViewById(R.id.tv_beltline);
        v.tv_problem=(TextView) convertView.findViewById(R.id.tv_problem);
        return convertView;
    }

    @Override
    protected void onReachBottom() {

    }

    @Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        DeviceRepairRecordEntity bean = getItem(position);

        switch (view.getId()) {
            case R.id.btn_detail:
                if (actionClickListener != null) {
                    actionClickListener.onDetailClick(bean);
                }
                break;
            case R.id.btn_del:
                if (actionClickListener != null) {
                    actionClickListener.onDelClick(bean);
                }
                break;
            case R.id.btn_check:
                if (actionClickListener != null) {
                    actionClickListener.onCheckClick(bean);
                }
                break;
            case R.id.btn_repair:
                if (actionClickListener != null) {
                    actionClickListener.onRepairClick(bean);
                }
                break;
        }
    }
    @Override
    public void onCheckedChanged(CompoundButton view, boolean b) {
        int position = (Integer) view.getTag();
        DeviceRepairRecordEntity bean = getItem(position);
        bean.setSelect_status(b);
    }
}
